<style lang="less">
	.hmComm{
		.ivu-modal {
			width: 660px !important;
		}
	}
</style>
<template>
	<Modal v-model="show" :title="editTitle" class="hmComm"  :mask-closable="false">
		<Form ref="hmCommForm" :model="hmCommForm" :rules="hmCommFormRule" :label-width="100">
					<FormItem label="评论用户ID" prop="user_id">
						<Input v-bind:disabled="disabled"  type="text" :maxlength=50 v-model="hmCommForm.userId" placeholder="请输入评论用户ID"/>
					</FormItem>
					<FormItem label="用户头像图片" prop="user_img">
						<Input v-bind:disabled="disabled"  type="text" :maxlength=50 v-model="hmCommForm.userImg" placeholder="请输入用户头像图片"/>
					</FormItem>
					<FormItem label="评论内容" prop="content">
						<Input v-bind:disabled="disabled"  type="text" :maxlength=50 v-model="hmCommForm.content" placeholder="请输入评论内容"/>
					</FormItem>
					<FormItem label="技术评分" prop="technical_score">
						<Input v-bind:disabled="disabled"  type="text" :maxlength=50 v-model="hmCommForm.technicalScore" placeholder="请输入技术评分"/>
					</FormItem>
					<FormItem label="服务评分" prop="service_score">
						<Input v-bind:disabled="disabled"  type="text" :maxlength=50 v-model="hmCommForm.serviceScore" placeholder="请输入服务评分"/>
					</FormItem>
					<FormItem label="是否匿名评论 1是0否" prop="anonymous">
						<Input v-bind:disabled="disabled"  type="text" :maxlength=50 v-model="hmCommForm.anonymous" placeholder="请输入是否匿名评论 1是0否"/>
					</FormItem>
		</Form>
		<div slot="footer">
			<Button type="text" @click="show=false">取消</Button>
			<Button type="primary" :loading="loading" @click="handSubmit" v-if="!disabled">提交</Button>
		</div>
	</Modal>
</template>
<script>
	import {addHmComm,updateHmComm,getHmComm} from '@/api/hmzd/hmComm'
	import { getDictDataByType } from '@/api/index';
	export default {
		name: "updateHmComm",
		props: {
			value: {
				type: Boolean,
				default: false
			},
			HmCommId: {
				type: String
			},
			modalTitle: {
				type: String
			}
		},
		data() {
			return {
				show: this.value,
				editTitle: this.modalTitle,
				loading: true,
				disabled:false,
				hmCommForm: {
					userId:'',
					userImg:'',
					content:'',
					technicalScore:'',
					serviceScore:'',
					anonymous:'',
				},
				hmCommFormRule: this.getHmCommFormRule()
			}
		},
		methods: {
			handSubmit() {
				this.$refs['hmCommForm'].validate((valid) => {
					this.loading = true;
					if (valid) {
						if(this.HmCommId!=null&&this.HmCommId.trim().length>0){
							this.hmCommForm.id=this.HmCommId;
							updateHmComm(this.hmCommForm).then(res => {
								if (res.success) {
									this.closeModal(false);
									this.$emit('handSearch');
									this.$Message.success('保存成功');
								}else{
									this.$Message.error(res.msg);
								}
							}).finally(() => {
								this.loading = false;
							});
						}else{
							addHmComm(this.hmCommForm).then(res => {
								if (res.success) {
									this.closeModal(false);
									this.$emit('handSearch');
									this.$Message.success('保存成功');
								}else{
									this.$Message.error(res.msg);
								}
							}).finally(() => {
								this.loading = false;
							});
						}
					} else {
							this.loading = false;
						this.$Message.error('表单验证不通过！');
					}
				});
			},
			closeModal(val) {
				this.$emit('input', val);
			},
			initForm(){
				this.hmCommForm= {
					userId:'',
					userImg:'',
					content:'',
					technicalScore:'',
					serviceScore:'',
					anonymous:'',
				};
			},
			getHmCommFormRule() {
				return {
					userId: [
						{required: true, message: '评论用户ID不能为空！', trigger: 'blur',pattern: /.+/ },
					],
					userImg: [
						{required: true, message: '用户头像图片不能为空！', trigger: 'blur',pattern: /.+/ },
					],
					content: [
						{required: true, message: '评论内容不能为空！', trigger: 'blur',pattern: /.+/ },
					],
					technicalScore: [
						{required: true, message: '技术评分不能为空！', trigger: 'blur',pattern: /.+/ },
					],
					serviceScore: [
						{required: true, message: '服务评分不能为空！', trigger: 'blur',pattern: /.+/ },
					],
					anonymous: [
						{required: true, message: '是否匿名评论 1是0否不能为空！', trigger: 'blur',pattern: /.+/ },
					],
				}
			}
		},
		watch: {
			value(val) {
				this.show = val;
			},
			show(val) {
				this.initForm();
				this.loading=false;
				this.editTitle = this.modalTitle;
				if(this.editTitle == "查看"){
					this.disabled = true;
				}
				else{
					this.disabled = false;
				}
				if(val) {
					this.$refs['hmCommForm'].resetFields();
					this.$refs['hmCommForm'].id = null;
					if(this.HmCommId!=null&&this.HmCommId.trim().length>0){
						getHmComm({id: this.HmCommId}).then(res => {
								if (res.success) {
									 this.hmCommForm = res.data;
							} else {
								this.$Message.error(res.msg);
							}
						});
					}
				} else {
					this.closeModal(val);
				}
			}
		}
	}
</script>
